package com.edevp.common.web.handler;

import com.edevp.common.core.dto.R;
import com.edevp.common.core.exception.EngineException;
import com.edevp.common.core.exception.ExceptionCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * @author Blueeyedboy
 * @create 2022-07-05 5:52 PM
 **/

@ControllerAdvice
@Slf4j
public class WebExceptionHandler {

	@org.springframework.web.bind.annotation.ExceptionHandler(EngineException.class)
	@ResponseBody
	public R handleStudentException(HttpServletRequest request, EngineException ex) {
		ex.printStackTrace();
		log.error("EngineException code:{},msg:{}",ex.getCode(),ex.getMsg());
		return R.error(ex.getCode(),ex.getMsg());
	}

	@org.springframework.web.bind.annotation.ExceptionHandler(RuntimeException.class)
	@ResponseBody
	public R handleException(HttpServletRequest request, RuntimeException ex) {
		log.error("RuntimeException error: ",ex);
		return R.error(ExceptionCode.SERVER_ERROR,ex.getMessage());
	}

	@org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
	@ResponseBody
	public R handleException(HttpServletRequest request, Exception ex) {
		R response;
		log.error("Exception error: ",ex);
		response = R.error(ExceptionCode.SERVER_ERROR,ex.getMessage());
		return response;
	}
}
